﻿using MyBasicLib;
using MyBasicLib.Utilities;
using MyBasicLib.Utilities.Log;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http.Filters;

namespace MyBasicLib.WebApi
{
    public class ExceptionLogFilterAttribute : ExceptionFilterAttribute
    {
        static FileLogger logger = new FileLogger() { FilePath = "log\\", MinLogLevel = LogLevel.Warning };
        public override void OnException(HttpActionExecutedContext context)
        {

            context.Response = context.Request.CreateResponse(HttpStatusCode.BadRequest, context.Exception.Message);
            if (context.Exception is ValidationException)
            {
                logger.Warning(context.Request.RequestUri.PathAndQuery + "\r\n" + context.Exception.Message);
                return;
            }
            logger.Error(context.Request.RequestUri.PathAndQuery + "\r\n" + context.Exception.ToString());
        }
    }
}